package com.rongxun.api.interceptor;

import com.rongxun.api.AppSysInfoManager;
import com.rongxun.api.utils.FlyLog;

import java.io.IOException;
import java.net.URLEncoder;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by nelon on 17-10-11.
 */

public class TokenInterceptor implements Interceptor {

    @Override
    public synchronized Response intercept(Chain chain) throws IOException {

        Request original = chain.request();
        Request.Builder requestBuilder = original.newBuilder()
                .header("Content-Type", "application/json")
                .header("Accept", "application/json");


        if (AppSysInfoManager.getInstance().getVersionName() != null) {

            String appVersion = AppSysInfoManager.getInstance().getVersionName().isEmpty() ?
                    "channelregistertest1" : AppSysInfoManager.getInstance().getVersionName();
            FlyLog.d(appVersion);
            requestBuilder.header("appVersion", appVersion);
        }
        if (AppSysInfoManager.getInstance().getAppName() != null) {

            String appName = URLEncoder.encode(AppSysInfoManager.getInstance().getAppName(), "UTF-8");
            FlyLog.d(appName);

            requestBuilder.header("appName", appName);

        }
        if (AppSysInfoManager.getInstance().getChannel() != null) {
            String appChannelCode = URLEncoder.encode(AppSysInfoManager.getInstance().getChannel(), "UTF-8");
            FlyLog.d(appChannelCode);

            requestBuilder.header("appChannelCode", appChannelCode);
        }

        if (AppSysInfoManager.getInstance().getAppSystem() != null) {
            String appSystem = "android" + AppSysInfoManager.getInstance().getAppSystem();
            FlyLog.d(appSystem);

            requestBuilder.header("appSystem", appSystem);
        }
        if (AppSysInfoManager.getInstance().getDeviceBrand() != null) {
            String appPhoneMake = AppSysInfoManager.getInstance().getDeviceBrand();
            FlyLog.d(appPhoneMake);

            requestBuilder.header("appPhoneMake", appPhoneMake);
        }
        if (AppSysInfoManager.getInstance().getUserId() != null) {
            String gwUserId = URLEncoder.encode(AppSysInfoManager.getInstance().getUserId(), "UTF-8");
            FlyLog.d(gwUserId);

            requestBuilder.header("gwUserId", gwUserId);
        }
        if (AppSysInfoManager.getInstance().getIMEI() != null) {
            String appEquipmentId = AppSysInfoManager.getInstance().getIMEI();
            FlyLog.d(appEquipmentId);

            requestBuilder.header("appEquipmentId", appEquipmentId);
        }

        if (AppSysInfoManager.getInstance().getPackageName() != null) {
            String packageName = AppSysInfoManager.getInstance().getPackageName();
            FlyLog.d(packageName);

            requestBuilder.header("packageName", AppSysInfoManager.getInstance().getPackageName());
        }

        Request request = requestBuilder
                .build();
        return chain.proceed(request);
    }
}

